(function(){
    angular.module('app')
        .directive('uiResize', countCtrl);
    function countCtrl(){
        return {
            restrict : 'AE',
            link : function( scope, element, attrs){
                element.on('mousedown', function(event) {
                    event.preventDefault();

                    scope.aside=document.querySelector('#aside');
                    document.addEventListener('mousemove',mousemove);
                    document.addEventListener('mouseup', mouseup);
                });

                function mousemove(event) {

                    //remove aside's width
                    var asideWidth=parseInt(document.querySelector('#aside').offsetWidth);

                    var x = event.pageX-asideWidth;

                    if (attrs.resizerMin && x < attrs.resizerMin) {
                        x = parseInt(attrs.resizerMin);
                    }
                    element.css({
                        left: x + 'px'
                    });

                    document.querySelector(attrs.resizerLeft).style.width=x+"px";
                }

                function mouseup() {
                    document.removeEventListener('mousemove', mousemove);
                    document.removeEventListener('mouseup', mouseup);
                }
            }
        }
    }
})();
